<template>
  <div class="login-wrap">
    <div class="title">
      music 后台管理登陆
    </div>
    <div class="login">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="用户名"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="密码"/>
        </el-form-item>
        <div class="login-button">
          <el-button @click="loginStatus" type="primary">登陆</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {getLoginStatus, loginTest} from '../api/index'

export default {
  name: 'Login',
  data: function () {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true, message: '请输入用户名', trigger: 'blur'
          }
        ],
        password: [
          {
            required: true, message: '请输入密码', trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    loginStatus: function () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let data = {
            'username': this.ruleForm.username,
            'password': this.ruleForm.password
          }
          getLoginStatus(data).then(res => {
            if (res.code === 200) {
              localStorage.setItem('SaToken', res.data.token)
              this.$message.success('登录成功,欢迎回来' + res.data.username)
              localStorage.setItem('userName', res.data.username)
              this.$router.push('Info')
              console.log(res)
            } else {
              this.$message.error('账号或密码错误')
            }
          })
        } else {
          return false
        }
      })
    },
    loginTest() {
      loginTest().then(res => {
        console.log(res)
      })
    }
  }
}
</script>

<style scoped>
  .login-wrap{
    position: relative;
    background: url("../assets/img/background.jpg") fixed center;
    width: 100%;
    height: 100%;
  }
  .title {
    position: absolute;
    top: 50%;
    width: 100%;
    margin-top: -230px;
    color: #fff;
    font-size: 30px;
    font-weight: 600;
    text-align: center;
  }
  .login{
    position: absolute;
    left: 50%;
    top: 50%;
    width: 300px;
    height: 160px;
    margin-left: -190px;
    margin-top: -150px;
    padding: 40px;
    border-radius: 5px;
    background: #fff;
  }
  .login-button{
    text-align: center;
  }
  .login-button button{
    width: 100%;
    height: 100%;
  }
</style>
